/*
Ajax 三级省市联动
日期：2012-7-18

settings 参数说明
-----
url:省市数据josn文件路径
prov:默认省份
city:默认城市
dist:默认地区（县）
nodata:无数据状态
required:必选项
------------------------------ */
//(function($){
	/*options = {
		countryEditers:[{},{}...],
		provEditers:[{},{}...],
		cityEditers:[{},{}...],
		distEditers:[{},{}...],
		countroy:
		prov:
		city:
		dist:
	}*/
	function citySelect(options){
		var city_json;
		var countrys=[];
		var provs = [];
		var citys = [];
		var dists = [];
		var curCountryIndex =-1,curProvIndex = -1,curCityIndex = -1,curDistIndex = -1;
		var clearValueAndText = function(editor){
			editor.setValue(null);
			editor.setText(null);
		}
		
		var getClearTag = function(value,arr,curEditor){
			var clearTag = true;
			var curIndex = -1;
			if(value){
				for(var i = 0 ;i<arr.length;i++){
					if(arr[i].id == value){
						curIndex = i;
						clearTag = false;
						break;
					}
				}
			}
			if(clearTag && value && value.length>0){
				clearValueAndText(curEditor);
			}
			return {clearTag:clearTag,curIndex:curIndex};
		}
		// 设置省
		var prov_start=function(value,text){
			var clearTagObj = getClearTag(value,countrys,this);
			var clearTag = clearTagObj.clearTag;
			curCountryIndex = clearTagObj.curIndex;
			/*if(value){
				for(var i = 0 ;i<countrys.length;i++){
					if(countrys[i].id == value){
						curCountryIndex = i;
						clearTag = false;
						break;
					}
				}
			}
			if(clearTag && value && value.length>0){
				clearValueAndText(this);
			}*/
			provs = [];
			if(!clearTag && curCountryIndex>=0 && typeof(city_json.countrylist[curCountryIndex].citylist)!="undefined"){
				$.each(city_json.countrylist[curCountryIndex].citylist,function(i,f_prov){
					provs.push({id:f_prov.p,text:f_prov.p});
				});
			}
			$.each(options.provEditers,function(i,editor){
				if(clearTag)clearValueAndText(editor);
				editor.set('data',provs);
			});
		};
		//设置城市
		var city_start = function(value,text){
			var clearTagObj = getClearTag(value,provs,this);
			var clearTag = clearTagObj.clearTag;
			curProvIndex =  clearTagObj.curIndex;
			
			/*var clearTag = true;
			if(value){
				for(var i = 0 ;i<provs.length;i++){
					if(provs[i].id == value){
						curProvIndex = i;
						clearTag = false;
						break;
					}
				}
			}*/
			
			citys = [];
			if(!clearTag && curProvIndex>=0 && typeof(city_json.countrylist[curCountryIndex].citylist[curProvIndex].c)!="undefined"){
				$.each(city_json.countrylist[curCountryIndex].citylist[curProvIndex].c,function(i,f_city){
					citys.push({id:f_city.n,text:f_city.n});
				});
			}
			$.each(options.cityEditers,function(i,editor){
				if(clearTag)clearValueAndText(editor);
				editor.set('data',citys);
			});
		}
		
		//设置地区（县）
		var dist_start =  function(value,text){
			
			var clearTagObj = getClearTag(value,citys,this);
			var clearTag = clearTagObj.clearTag;
			curCityIndex =  clearTagObj.curIndex;
			
			/*var clearTag = true;
			if(value){
				for(var i = 0 ;i<citys.length;i++){
					if(citys[i].id == value){
						curCityIndex = i;
						clearTag = false;
						break;
					}
				}
			}*/
			
			dists = [];
			if(!clearTag && curCityIndex>=0 && typeof(city_json.countrylist[curCountryIndex].citylist[curProvIndex].c[curCityIndex].a)!="undefined"){
				$.each(city_json.countrylist[curCountryIndex].citylist[curProvIndex].c[curCityIndex].a,function(i,f_dist){
					dists.push({id:f_dist.s,text:f_dist.s});
				});
			}
			$.each(options.distEditers,function(i,editor){
				if(clearTag)clearValueAndText(editor);
				editor.set('data',dists);
			});
		}
		
		var init=function(){
		if(options.provEditers){
			$.each(options.provEditers,function(i,editor){
				editor.bind('selected', city_start/*function (value,text) { 
			 	city_start(value);
			 }*/);
			});
		}
		if(options.cityEditers){
			$.each(options.cityEditers,function(i,editor){
				editor.bind('selected',dist_start /*function (value,text) {
			 	    dist_start(value);
			 }*/);
			});
		}
				//初始化国家选择框数据
		if(options.countryEditers){
			$.each(city_json.countrylist,function(i,prov){
				countrys.push({id:prov.co,text:prov.co});
			});
			$.each(options.countryEditers,function(i,editor){
				editor.bind('selected',prov_start /*function (value,text) { 
			 	prov_start(value);
			 }*/);
			 editor.set('data',countrys);
			});
		}
		
		/*	// 若有传入省份与市级的值，则选中。（setTimeout为兼容IE6而设置）
			setTimeout(function(){
				if(settings.prov!=null){
					prov_obj.val(settings.prov);
					cityStart();
					setTimeout(function(){
						if(settings.city!=null){
							city_obj.val(settings.city);
							distStart();
							setTimeout(function(){
								if(settings.dist!=null){
									dist_obj.val(settings.dist);
								};
							},1);
						};
					},1);
				};
			},1);*/
		
			
		};
		$.getJSON("/resources/js/city.js",function(json){
				city_json=json;
				init();
			});
		
	}
	
	//$.fn.citySelect=function(settings){
	/*
		if(this.length<1){return;};

		// 默认值
		settings=$.extend({
			url:"/resources/js/city.js",
			prov:null,
			city:null,
			dist:null,
			nodata:null,
			required:true
		},settings);

		var box_obj=this;
		var prov_obj=box_obj.find(".prov");
		var city_obj=box_obj.find(".city");
		var dist_obj=box_obj.find(".dist");
		var prov_val=settings.prov; 
		var city_val=settings.city;
		var dist_val=settings.dist;
		var select_prehtml=(settings.required) ? "" : "<option value=''>请选择</option>";
		var city_json;

		// 赋值市级函数
		var cityStart=function(){
			var prov_id=prov_obj.get(0).selectedIndex;
			if(!settings.required){
				prov_id--;
			};
			city_obj.empty().attr("disabled",true);
			dist_obj.empty().attr("disabled",true);

			if(prov_id<0||typeof(city_json.citylist[prov_id].c)=="undefined"){
				if(settings.nodata=="none"){
					city_obj.css("display","none");
					dist_obj.css("display","none");
				}else if(settings.nodata=="hidden"){
					city_obj.css("visibility","hidden");
					dist_obj.css("visibility","hidden");
				};
				return;
			};
			
			// 遍历赋值市级下拉列表
			temp_html=select_prehtml;
			$.each(city_json.citylist[prov_id].c,function(i,city){
				temp_html+="<option value='"+city.n+"'>"+city.n+"</option>";
			});
			city_obj.html(temp_html).attr("disabled",false).css({"display":"","visibility":""});
			distStart();
		};

		// 赋值地区（县）函数
		var distStart=function(){
			var prov_id=prov_obj.get(0).selectedIndex;
			var city_id=city_obj.get(0).selectedIndex;
			if(!settings.required){
				prov_id--;
				city_id--;
			};
			dist_obj.empty().attr("disabled",true);

			if(prov_id<0||city_id<0||typeof(city_json.citylist[prov_id].c[city_id].a)=="undefined"){
				if(settings.nodata=="none"){
					dist_obj.css("display","none");
				}else if(settings.nodata=="hidden"){
					dist_obj.css("visibility","hidden");
				};
				return;
			};
			
			// 遍历赋值市级下拉列表
			temp_html=select_prehtml;
			$.each(city_json.citylist[prov_id].c[city_id].a,function(i,dist){
				temp_html+="<option value='"+dist.s+"'>"+dist.s+"</option>";
			});
			dist_obj.html(temp_html).attr("disabled",false).css({"display":"","visibility":""});
		};

		var init=function(){
			// 遍历赋值省份下拉列表
			temp_html=select_prehtml;
			$.each(city_json.citylist,function(i,prov){
				temp_html+="<option value='"+prov.p+"'>"+prov.p+"</option>";
			});
			prov_obj.html(temp_html);

			// 若有传入省份与市级的值，则选中。（setTimeout为兼容IE6而设置）
			setTimeout(function(){
				if(settings.prov!=null){
					prov_obj.val(settings.prov);
					cityStart();
					setTimeout(function(){
						if(settings.city!=null){
							city_obj.val(settings.city);
							distStart();
							setTimeout(function(){
								if(settings.dist!=null){
									dist_obj.val(settings.dist);
								};
							},1);
						};
					},1);
				};
			},1);

			// 选择省份时发生事件
			prov_obj.bind("change",function(){
				cityStart();
			});

			// 选择市级时发生事件
			city_obj.bind("change",function(){
				distStart();
			});
		};

		// 设置省市json数据
		if(typeof(settings.url)=="string"){
			$.getJSON(settings.url,function(json){
				city_json=json;
				init();
			});
		}else{
			city_json=settings.url;
			init();
		};
	*///};
//})(jQuery);